package cn.i96688.scanclient.service.impl;


import com.alibaba.fastjson.JSONObject;
import cn.i96688.scanclient.bean.MerInfo;
import cn.i96688.scanclient.bean.PayLog;
import cn.i96688.scanclient.bean.Status;
import cn.i96688.scanclient.service.ScanPayService;
import cn.i96688.scanclient.utils.BarcodeUtil;
import cn.i96688.scanclient.utils.ConfParams;
import cn.i96688.scanclient.utils.Constants;
import cn.i96688.scanclient.utils.HttpRequest;

public class ScanPayServiceImpl implements ScanPayService {

	@Override
	public JSONObject pay(String payableAmount,String barcode) {
		
		String payType=BarcodeUtil.getPayType(barcode);
		if(payType.length()==0){
			return Status.fail("不支持的条码类型："+barcode);
		}
		JSONObject merInfo=MerInfo.getInstance().getMerInf();
		
		JSONObject params=new JSONObject();
		params.put("deviceCode", MerInfo.getInstance().getDeviceCode());//设备编码
		params.put("payableAmount", payableAmount);
		params.put("merId", merInfo.getString("merId"));//商户ID
		params.put("payType", payType);//支付类别：支付宝、微信
		params.put("payCode", barcode);//条形码
		
		
		String url=ConfParams.getValueByKey("server.url")+Constants.URI_PAY;
		try {
			String str=HttpRequest.sendPost(url, params.toString());
			JSONObject respJson=JSONObject.parseObject(str);
			return respJson;
		} catch (Exception e) {
			PayLog.getInstance().addLog("支付出错："+barcode);
			PayLog.getInstance().addLogs(e);
			return Status.fail("系统错误，支付失败，稍候重试");
		}
	}

	@Override
	public JSONObject queryOrder(String orderNo) {
		if(orderNo.length()==18){
				return Status.fail("订单号为空，支付查询异常");
		}
		
		JSONObject params=new JSONObject();
		params.put("orderNo", orderNo);
		
		String url=ConfParams.getValueByKey("server.url")+Constants.URI_ORDER_QUERY;
		try {
			String str=HttpRequest.sendPost(url, params.toString());
			JSONObject respJson=JSONObject.parseObject(str);
			return respJson;
		} catch (Exception e) {
			PayLog.getInstance().addLog("订单查询出错："+orderNo);
			PayLog.getInstance().addLogs(e);
			return Status.fail("支付查询异常，支付失败，稍候重试");
		}
	}

	@Override
	public JSONObject queryOrders() {
		JSONObject params=new JSONObject();
		params.put("merId", MerInfo.getInstance().getMerInf().getString("merId"));
		params.put("deviceCode", MerInfo.getInstance().getDeviceCode());
		String url=ConfParams.getValueByKey("server.url")+Constants.URI_ORDER_LIST;
		try {
			String str=HttpRequest.sendPost(url, params.toString());
			JSONObject respJson=JSONObject.parseObject(str);
			return respJson;
		} catch (Exception e) {
			PayLog.getInstance().addLog("当前设置查询所有订单出错");
			PayLog.getInstance().addLogs(e);
			return Status.fail("支付查询异常，支付失败，稍候重试");
		}
	}

}
